package com.hsnn.medstgmini.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hsnn.medstgmini.base.dict.model.DicSys;
import com.hsnn.medstgmini.pro.base.model.BaseGoods;
import com.hsnn.medstgmini.pro.base.service.BaseGoodsManager;
import com.hsnn.medstgmini.pro.project.model.BidProject;

@Component
@Scope("singleton")
public class LoadDataUtil {
	
	private static final Logger log = Logger.getLogger(LoadDataUtil.class);

	private static BaseGoodsManager baseGoodsManager;
	
	
	private static Map<String, BaseGoods> baseGoodsMap;
	private static Map<String, BidProject> bidProjectMap;
	private static Map<String, Object[]> stdDrugMap;
	private static Map<String,DicSys> dicSysMap;
	
	
	 public static void setBaseGoodsManager(BaseGoodsManager baseGoodsManager) {
		LoadDataUtil.baseGoodsManager = baseGoodsManager;
	}

	public static void init() {
		 baseGoodsMap = new HashMap<String, BaseGoods>();
		 bidProjectMap = new HashMap<String, BidProject>();
		 stdDrugMap = new HashMap<String, Object[]>();
		 dicSysMap = new HashMap<String, DicSys>();
	    if (baseGoodsManager != null) {
	    	List<BaseGoods> goodsList = baseGoodsManager.getBaseGoodsList();
	    	if (null != goodsList && goodsList.size() > 0){
	    		for (BaseGoods baseGoods : goodsList) {
					baseGoodsMap.put(baseGoods.getGoodsCode(), baseGoods);
				}
	    	}
			List<BidProject> bidProjectList = baseGoodsManager.getBidProjectList();
			if (null != bidProjectList && bidProjectList.size() > 0) {
				for (BidProject bidProject : bidProjectList) {
					bidProjectMap.put(bidProject.getProjCode(), bidProject);
				}
			}
			List<Object[]> stdDrugList = baseGoodsManager.getStdDrugList();
			if (null != stdDrugList && stdDrugList.size() > 0){
				for (Object[] data : stdDrugList){
					stdDrugMap.put(data[1].toString(), data);
				}
			}
			List<DicSys> dicSysList = baseGoodsManager.getDicSysList();
			if (null != dicSysList && dicSysList.size() > 0) {
				for (DicSys data : dicSysList) {
					dicSysMap.put(data.getDicName(), data);
				}
			}
	    } else {
	    	log.error("dicSysManager is null.");
	    }	
	}
	 
	 public static BaseGoods getBaseGoods(String goodsCode) {
			if (goodsCode != null && StringUtils.isNotBlank(goodsCode)) {
				return baseGoodsMap.get(goodsCode);
			}
			return null;
		}
	 public static BidProject getBidProject(String prjCode) {
			if (prjCode != null && StringUtils.isNotBlank(prjCode)) {
				return bidProjectMap.get(prjCode);
			}
			return null;
		}
	 public static Object[] getStdDrug(String drugCode) {
			if (drugCode != null && StringUtils.isNotBlank(drugCode)) {
				return stdDrugMap.get(drugCode);
			}
			return null;
		}
	 
	 public static DicSys getDicSys(String dicName) {
			if (dicName != null && StringUtils.isNotBlank(dicName)) {
				return dicSysMap.get(dicName);
			}
			return null;
		}
}
